<?php

namespace app\index\model\tan;

use think\Model;
use think\db;
/**
 * 消费记录log
 */
class CardBalanceLog extends Model
{
    
    /**
     * 显示搜索
     * @param array $search
     * @param int   $id     userid
     * @return type
     */

    public function SearchShow($search,$id){
        $where=[
             "c.user_id"=>$id,
             'c.state'=>0,
         ]; 
        if(!empty($search)){
            array_key_exists('day', $search) ? $day = $search['day'] : $day = 0;
            array_key_exists('startdate', $search) ? $startdate = $search['startdate'] : $startdate = 0;
            array_key_exists('enddate', $search) ? $enddate = $search['enddate'] : $enddate = 0;
            if($day == 1){
                $startdate = date('Y-m-d', strtotime('-7 days'));
                $where["or.createtime"] =[">=",$startdate] ;
            }elseif($day == 2){
                $startdate = date('Y-m-d', strtotime('-30 days'));
                $where["or.createtime"] =[">=",$startdate] ;
            }elseif(!empty($startdate) && empty($enddate)){
                $where["or.createtime"] =[">=",$startdate] ;
            }elseif(empty($startdate) && !empty($enddate)){
                $where["or.createtime"] =["<=",$enddate] ;
            }elseif(!empty($startdate) && !empty($enddate)) {
                $where["or.createtime"] =["BETWEEN","$startdate,$enddate"] ;
            }
        }
        $filed = 'c.id,c.batchcode,c.createtime,c.description,c.money,c.operating,or.createtime or_createtime,or.pay_reatetime,o.shipping_code,'
                . 'or.product_name,'//or.required_currency,or.obtained_currency,'
                . 'or.cost_price,or.price,or.peer_price';
        $card_balance_log = $this->alias('c')
                            ->join('orders o','c.batchcode = o.batchcode','LEFT')
                            ->join('orders_info or', 'c.batchcode = or.batchcode', 'LEFT')
                            ->join('product p','or.product_id = p.id','LEFT')
                            ->where($where)
                            ->order('c.id desc')
                            ->field($filed)
                            ->paginate()->toarray();
            foreach ($card_balance_log['data'] as $key => $value) {
                if($value['operating'] == 1){ //支出
                    $ex_money = $value['money'];
                    $in_money = 0;
                }elseif($value['operating'] == 0){ //支出
                    $in_money = $value['money'];
                    $ex_money = 0;
                }
                $card_balance_log['data'][$key]['money'] = [
                    'ex_money' => $ex_money,
                    'in_money' => $in_money,
                ];
            }
        $sum_money = $this->sum_income_expenditure($where);
        $card_balance_log['income_sum'] = $sum_money['income_sum'];
        $card_balance_log['expenditure_sum'] = $sum_money['expenditure_sum'];
        return $card_balance_log;
    }
    
    /**
     * 计算总收入和总支出
     */
    public function sum_income_expenditure($where='') {
        $income_sum = $this->alias('c')
                    ->join('orders o','c.batchcode = o.batchcode','LEFT')
                    ->join('orders_info or', 'c.batchcode = or.batchcode', 'LEFT')
                    ->join('product p','or.product_id = p.id','LEFT')
                    ->where($where)
                    ->where('operating',0)
                    ->sum('money');
        $expenditure_sum = $this->alias('c')
                    ->join('orders o','c.batchcode = o.batchcode','LEFT')
                    ->join('orders_info or', 'c.batchcode = or.batchcode', 'LEFT')
                    ->join('product p','or.product_id = p.id','LEFT')
                    ->where($where)
                    ->where('operating',1)
                    ->sum('money');
        return ['income_sum'=>$income_sum,'expenditure_sum'=>$expenditure_sum];
    }
    /**
     * 
     * @param type $bool 是否成功，成功
     */
    public function insert_($bool){
        $this->transaction(function() use ($bool){
            if($bool == true){
                
            }
        });
    }
}
